//
//  NBATeamViewController.m
//  movement
//
//  Created by lanou on 16/5/12.
//  Copyright © 2016年 lanou. All rights reserved.
//  http://api.avatardata.cn/Nba/NomalRace?key=60bab6fd150745158d7a681fe9534df6

#import "NBATeamViewController.h"
#import "NetworkRequest.h"
#import "TeamCollectionViewCell.h"
#import "TeamModel.h"
#import "TeamDetailViewController.h"

@interface NBATeamViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) UICollectionView *collection;

@end

@implementation NBATeamViewController
- (NSMutableArray *)array {
    if (_array == nil) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 5; // 最小行间距
    layout.minimumInteritemSpacing = 5; // 最小列间距
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 10) / 3 - 5, (self.view.frame.size.width - 10) / 3);
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    self.collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collection];
    [self.collection registerNib:[UINib nibWithNibName:@"TeamCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"acell"];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self getData];
    
    
}
// 数据解析
- (void)getData {
    
    [NetworkRequest getUrl:@"http://api.avatardata.cn/Nba/NomalRace?key=60bab6fd150745158d7a681fe9534df6" CallBack:^(id data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = [NSArray arrayWithArray:dict[@"result"][@"teammatch"]];
        for (int i = 1; i < 31; i++){
            [arr[i - 1] setValue:[NSString stringWithFormat:@"%d.jpg", i] forKey:@"images"];
        }
        for (NSDictionary *dic in arr) {
        TeamModel *model = [[TeamModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.array addObject:model];
    }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collection reloadData];
        });
    }];
}

// UICollectionDelegate & UICollectionDataSource
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TeamCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"acell" forIndexPath:indexPath];
    TeamModel *model = [[TeamModel alloc] init];
    model = self.array[indexPath.row];
    cell.model = model;
    cell.imagesView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", (long)indexPath.row + 1]];
//    cell.label.text = @"wqe";
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    TeamDetailViewController *TeamVC = [[TeamDetailViewController alloc] init];
    TeamVC.number = indexPath.row;
    [self.navigationController pushViewController:TeamVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
